package controller;

import implementor.FollowUpActionImp;
import implementor.LeadsImp;
import implementor.PriorityImp;
import implementor.SalesPersonImp;
import implementor.CustomerInfoImp;
import implementor.FeedbackImp;
import implementor.InfoSourceImp;
import implementor.ProductImp;
import implementor.InquiryStateImp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.Timestamp;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import daoInterfaces.FollowUpActionDao;
import daoInterfaces.LeadsDao;
import daoInterfaces.PriorityDao;
import daoInterfaces.SalesPersonDao;
import daoInterfaces.CustomerInfoDao;
import daoInterfaces.FeedbackDao;
import daoInterfaces.InfoSourceDao;
import daoInterfaces.ProductDao;
import daoInterfaces.InquiryStateDao;
import dbhandelling.DatabaseHelper;
import dto.Priority;
import dto.SalesPerson;
import dto.LeadDto;
import dto.Feedback;
import dto.InfoSource;
import dto.Product;
import dto.InquiryState;

import model.CustomerInfo;
import model.FollowUpAction;


import utility.ViewUtilites;
import view.leeds.CustomerInfoPanel;
import view.leeds.EnquiryFormView;
import view.leeds.OfficeUsePanel;
import view.leeds.RequirementsPanel;

public class EnquiryFormController implements ActionListener{
	private EnquiryFormView view;
	private CustomerInfo modelcust;
	private Product modelprod;
	private InfoSource modelinfo;
	private LeadDto modelenq;
	private Feedback modelfee;
	private InquiryState modelInquiryState;
	private SalesPerson modelattendedBy;
	private FollowUpAction followupAction;
	private Priority priority;
	

	public EnquiryFormController() {
	
	}
	
	


	public EnquiryFormController(EnquiryFormView view, CustomerInfo modelcust,
			Product modelprod, InfoSource modelinfo, LeadDto modelenq,
			Feedback modelfee, InquiryState modelInquiryState,
			SalesPerson modelattendedBy, FollowUpAction followupAction,
			Priority priority) 
	{
		super();
		this.view = view;
		this.modelcust = modelcust;
		this.modelprod = modelprod;
		this.modelinfo = modelinfo;
		this.modelenq = modelenq;
		this.modelfee = modelfee;
		this.modelInquiryState = modelInquiryState;
		this.modelattendedBy = modelattendedBy;
		this.followupAction = followupAction;
		this.priority = priority;
		view.actionListener(this);
	}

	


	/*
	 * @author Pradnya
	 */
	
	public EnquiryFormView getView() {
		return view;
	}




	public void setView(EnquiryFormView view) {
		this.view = view;
	}




	public CustomerInfo getModelcust() {
		return modelcust;
	}




	public void setModelcust(CustomerInfo modelcust) {
		this.modelcust = modelcust;
	}




	public Product getModelprod() {
		return modelprod;
	}




	public void setModelprod(Product modelprod) {
		this.modelprod = modelprod;
	}




	public InfoSource getModelinfo() {
		return modelinfo;
	}




	public void setModelinfo(InfoSource modelinfo) {
		this.modelinfo = modelinfo;
	}




	public LeadDto getModelenq() {
		return modelenq;
	}




	public void setModelenq(LeadDto modelenq) {
		this.modelenq = modelenq;
	}




	public Feedback getModelfee() {
		return modelfee;
	}




	public void setModelfee(Feedback modelfee) {
		this.modelfee = modelfee;
	}




	public InquiryState getModelInquiryState() {
		return modelInquiryState;
	}




	public void setModelInquiryState(InquiryState modelInquiryState) {
		this.modelInquiryState = modelInquiryState;
	}




	public SalesPerson getModelattendedBy() {
		return modelattendedBy;
	}




	public void setModelattendedBy(SalesPerson modelattendedBy) {
		this.modelattendedBy = modelattendedBy;
	}




	public FollowUpAction getFollowupAction() {
		return followupAction;
	}




	public void setFollowupAction(FollowUpAction followupAction) {
		this.followupAction = followupAction;
	}




	public Priority getPriority() {
		return priority;
	}




	public void setPriority(Priority priority) {
		this.priority = priority;
	}




	public void setComboReq()
	{
		ProductDao p=new ProductImp();
		Product[] product=p.retrieveAll();
		String[] productNames=new String[product.length+1];
		productNames[0]="--Select Product Names--";
		for(int i=1;i<=product.length;i++)
		{
			productNames[i]=product[i-1].getProductName();
		}
		ViewUtilites.set_comboBox(view.getRequirements().getComboBoxRequirement(),productNames);
		ViewUtilites.setAutoSuggestionsComboBox(view.getRequirements().getComboBoxRequirement());
	}
	
	public void setComboInfo()
	{
		InfoSourceDao is=new InfoSourceImp();
		InfoSource[] infosource=is.retrieveAll();
		String[] items=new String[infosource.length+1];
		items[0]="--Select Source of Information--";
		for(int i=1;i<=infosource.length;i++)
		{
			items[i]=infosource[i-1].getInfoSourceName();
		}
		ViewUtilites.set_comboBox(view.getRequirements().getComboBoxInformationBy(), items);
		ViewUtilites.setAutoSuggestionsComboBox(view.getRequirements().getComboBoxInformationBy());
	}
	
	public void setComboStatus()
	{
		InquiryStateDao status=new InquiryStateImp();
		InquiryState[] s=status.retrieveAll();
		String[] items=new String[s.length+1];
		items[0]="--Select Status--";
		for(int i=1;i<=s.length;i++)
		{
			items[i]=s[i-1].getStateName();
		}
		ViewUtilites.set_comboBox(view.getOfficeUse().getComboBoxStatus(), items);
		ViewUtilites.setAutoSuggestionsComboBox(view.getOfficeUse().getComboBoxStatus());
		
	}
	
	public void setComboAtten()
	{
		SalesPersonDao att=new SalesPersonImp();
		SalesPerson[] a=att.retrieveAll();
		String[] items=new String[a.length+1];
		items[0]="--Select SalesPerson Name--";
		for(int i=1;i<=a.length;i++)
		{
			items[i]=a[i-1].getSalesPersonName();
		}
		ViewUtilites.set_comboBox(view.getOfficeUse().getComboBoxAttendedBy(), items);
		ViewUtilites.setAutoSuggestionsComboBox(view.getOfficeUse().getComboBoxAttendedBy());
	}
	
	public void setComboPriority()
	{
		PriorityDao att=new PriorityImp();
		Priority[] a=att.retrieveAll();
		String[] items=new String[a.length+1];
		items[0]="--Select Priority Name--";
		for(int i=1;i<=a.length;i++)
		{
			items[i]=a[i-1].getPriorityName();
		}
		ViewUtilites.set_comboBox(view.getOfficeUse().getComboBoxPriority(), items);
		ViewUtilites.setAutoSuggestionsComboBox(view.getOfficeUse().getComboBoxPriority());
	}


	public void reactOnSave()
	{
		
		CustomerInfoPanel panelCustInfo=view.getCustomerInfo();
		RequirementsPanel panelReq=view.getRequirements();
		OfficeUsePanel panelOffice=view.getOfficeUse();
		
		
		String name= panelCustInfo.getTextFieldName().getText();
		String company=panelCustInfo.getTextFieldCompany().getText();
		String address=panelCustInfo.getTextAreaAddress().getText();
		String email=panelCustInfo.getTextFieldEmail().getText();
		double ph=Double.parseDouble(panelCustInfo.getTextFieldPhone().getText());
		modelcust=new CustomerInfo(0, name, address, company, email, ph);
		
		CustomerInfoDao impl=new CustomerInfoImp();
		try
		{
			impl.add(modelcust);
			JOptionPane.showMessageDialog(null, "Data successfully added");
		}
		catch(SQLIntegrityConstraintViolationException e)
		{
			JOptionPane.showMessageDialog(null, "Name already exist, please use another one");
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully added,Please try again !");
		
		}
		
		String req=panelReq.getComboBoxRequirement().getSelectedItem().toString();
		double bud=Double.parseDouble(panelReq.getTfbudget().getText());
		String info=panelReq.getComboBoxInformationBy().getSelectedItem().toString();
		String ref=panelReq.getComboBoxRequirement().getSelectedItem().toString();
		Date dates=ViewUtilites.convert(view.getTfDate().getText());
		String priorityName=panelOffice.getComboBoxPriority().getSelectedItem().toString();
		
		CustomerInfo c=impl.getLatest();
		int custId=c.getCustomerId();
		
		
		ProductDao product=new ProductImp();
		Product p=product.retrieve("productName", req);
		String prodcode=p.getProductCode();
		
		InfoSourceDao inf=new InfoSourceImp();
		InfoSource infos=inf.retrieve("infoSourceName", info);
		int sourceCode=infos.getInfoSourceCode();
		
		modelenq=new LeadDto(0, custId, bud, sourceCode, ref, dates, prodcode, 1, priorityName);
		LeadsDao imp=new LeadsImp();
		try
		{
			imp.add(modelenq);
			JOptionPane.showMessageDialog(null, "Data successfully added");
		}
	
		catch(SQLIntegrityConstraintViolationException e)
		{
			JOptionPane.showMessageDialog(null, "Name already exist, please use another one");
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully added,Please try again !");
		
		}
		
		String feedback=panelReq.getTextAreaFeedback().getText();
		modelfee=new Feedback(1, feedback);
		FeedbackDao f=new FeedbackImp();
		try
		{
			f.add(modelfee);
			JOptionPane.showMessageDialog(null, "Data successfully added");
		}
	
		catch(SQLIntegrityConstraintViolationException e)
		{
			JOptionPane.showMessageDialog(null, "Name already exist, please use another one");
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully added,Please try again !");
		
		}
			
		String comm=panelOffice.getTextAreaComments().getText();
		String staname=panelOffice.getComboBoxStatus().getSelectedItem().toString();
		String attendedBy=panelOffice.getComboBoxAttendedBy().getSelectedItem().toString();
		
		InquiryStateDao inq=new InquiryStateImp();
		InquiryState i=inq.retrieve("stateName", staname);
		int stateId=i.getStateId();
		
		SalesPersonDao sales=new SalesPersonImp();
		SalesPerson s=sales.retrieve("salesPersonName", attendedBy);
		int Id=s.getSalesPersonId();
		
		LeadsDao leadimp=new LeadsImp();
		LeadDto l=leadimp.getLatest();
		int enqCode=l.getEnquiryCode();
		
		Timestamp t=ViewUtilites.getTime();
		
		followupAction=new FollowUpAction(0, enqCode, 1, Id, comm, dates, t, "Registered", stateId);
		FollowUpActionDao imple=new FollowUpActionImp();
		try
		{
			imple.add(followupAction);
			JOptionPane.showMessageDialog(null, "Data successfully added");
		}
	
		catch(SQLIntegrityConstraintViolationException e)
		{
			JOptionPane.showMessageDialog(null, "Name already exist, please use another one");
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully added,Please try again !");
		
		}
			
	}


	@Override
	public void actionPerformed(ActionEvent ar) 
	{
		String k=ar.getActionCommand();
		if(k.contains("Save"))
		{
			reactOnSave();
		}
		
		if(k.contains("Cl"))
		{
			ViewUtilites.makeDateComponent(view.getTfDate());
		}
		
		if(k.contains("Sms"))
		{
			
		}
		
		if(k.contains("Mail"))
		{
			
		}
		
		if(k.contains("Print"))
		{
			
		}
	}
	
	
	

}
